#define ExceptionInfo ISOExceptionInfo
#define ColorInfo ISOColorInfo

#import <Foundation/Foundation.h>
#import "MagickWand.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	
	// printout ImageMagick version
	NSLog(@"%s", GetMagickVersion(nil));
	
	#define ThrowWandException(wand) \
	{ \
		char \
	*description; \
		\
	ExceptionType \
	severity; \
	\
		description=MagickGetException(wand,&severity); \
	(void) fprintf(stderr,"%s %s %u %s\n",GetMagickModule(),description); \
	description=(char *) MagickRelinquishMemory(description); \
	exit(-1); \
	}
	
	MagickWandGenesis();
	MagickWand * magick_wand= NewMagickWand();
	
	NSString * path = [[NSBundle mainBundle] resourcePath];
	
	/*
	 Read Image
	 */
	NSString *picpath=[path stringByAppendingString:@"/../../toloveru02.jpg"];
	MagickBooleanType status = MagickReadImage(magick_wand,[picpath UTF8String]);
	
	if (status == MagickFalse) {
		ThrowWandException(magick_wand);
		//return 0;
	}
	
	/*
	 Turn the images into a thumbnail sequence.
	 */
	MagickResetIterator(magick_wand);
	while (MagickNextImage(magick_wand) != MagickFalse){
		MagickResizeImage(magick_wand,106,80,LanczosFilter,1.0);
	}
	
	/*
	 Write the image then destroy it.
	 */
	NSString *outpath=[path stringByAppendingString:@"/../../toloveru02_t.jpg"];
	status=MagickWriteImages(magick_wand,[outpath UTF8String],MagickTrue);
	if (status == MagickFalse)
		ThrowWandException(magick_wand);
	magick_wand=DestroyMagickWand(magick_wand);
	MagickWandTerminus();
	
    [pool drain];
    return 0;
}
